package com.ruoyi.asset.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * 狮桥评估接口
 *
 * @author Liu
 * @date 2025-06-30
 */

@Slf4j
@Component
public class SQRequestUtils {

    private static String TEM_URL = "/uveh_saas_app/external_detection/query_templates";

    private static String TEM_DETAIL = "/uveh_saas_app/external_detection/find_detection_template";

    private static String SUBMIT = "/uveh_saas_app/external_detection/save_detection";

    private static String FIND_DETAIL = "/uveh_saas_app/external_detection/find_detection_detail";

    private static String QUERY_DETECTIONS = "/uveh_saas_app/external_detection/query_detections";

    @Autowired
    private SQSecret sqSecret;

    /**
     * 查询Id
     *
     * @param vin
     * @return
     */
    public String detections(String vin) {
        JSONObject data = new JSONObject();
//        data.set("vin", vin);
        data.set("detectionStatus", 1);
        data.set("pageNum", 1);
        data.set("pageSize", 10);
        String body = getDongHttpPost(QUERY_DETECTIONS).body(decodeParam(data)).execute().body();
        try {
            String decrypt = RSAUtils.decrypt(body);
            System.out.println(decrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";

    }

    /**
     * 查看
     *
     */
    public JSONObject findDetail(String detectionId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("detectionId",detectionId);
        String body = getDongHttpPost(FIND_DETAIL).body(decodeParam(jsonObject)).execute().body();
        try {
            String decrypt = RSAUtils.decrypt(body);
            JSONObject jsonObject1 = JSONUtil.parseObj(decrypt);
            return jsonObject1.getJSONObject("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加密
     *
     * @param jsonObject
     * @return
     */
    private String decodeParam(JSONObject jsonObject) {
        JSONObject parameter = new JSONObject();
        JSONObject param = new JSONObject();
        param.set("param", jsonObject);
        parameter.set("parameter", param);
        String encrypt = null;
        try {
            encrypt = RSAUtils.encrypt(parameter.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encrypt;
    }


    /**
     * 提交
     *
     * @return
     */
    public String submit(JSONObject data) {
        String body = getDongHttpPost(SUBMIT).body(decodeParam(data)).execute().body();
        try {
            String decrypt = RSAUtils.decrypt(body);
            JSONObject jsonObject = JSONUtil.parseObj(decrypt);
            JSONObject data1 = jsonObject.getJSONObject("data");
            return data1.getStr("detectionId");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getTemp() {
        String body = getDongHttpPost(TEM_URL).body(decodeParam(new JSONObject())).execute().body();
        try {
            String decrypt = RSAUtils.decrypt(body);
            System.out.println(decrypt);
            return JSONUtil.parseObj(decrypt).getJSONArray("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getTempDetail(String code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("templateId", code);

        String body = getDongHttpPost(TEM_DETAIL).body(decodeParam(jsonObject)).execute().body();
        try {
            String decrypt = RSAUtils.decrypt(body);
            return JSONUtil.parseObj(decrypt).getJSONObject("data").getJSONArray("templateInfo");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取请求对象
     *
     * @param url
     * @return
     */
    public HttpRequest getDongHttpPost(String url) {
        HttpRequest request = HttpRequest.post(sqSecret.getUrl() + url)
                .header("X-Shiqiao-App-Id", sqSecret.getAppId())
                .header("X-Shiqiao-Sec-Encryed", String.valueOf(false))
                .header("Content-Type", "application/json")
                .header("X-Shiqiao-Timestamp", String.valueOf(DateUtils.getNowDate().getTime()))
                .header("X-Shiqiao-Api-Version","1.0.0")
                .header("traceid", sqSecret.getTraceid());
        return request;
    }

}
